<?php

class ExcursionController extends CController
{
	public function actionIndex()
	{
		$this->setPageTitle('Экскурсии');
		
		$excursionTypes = CActiveRecordData::getArray(
			ExcursionType::model()->criteria('id', 'asc')->findAll(),
			array('id', 'title')
		);
		
		$this->render('index', array(
			'excursionTypes' => $excursionTypes
		));
	}
	
	public function actionGetdata()
	{
		$typeId = intval($_GET['type']);
		
		$start = (intval($_GET['start']) >= 0) ? intval($_GET['start']) : 0;
		$limit = (intval($_GET['limit']) >= 0) ? intval($_GET['limit']) : 20;
		$sort  = in_array(strtolower($_GET['sort']), array('id', 'title', 'price')) ? strtolower($_GET['sort']) : 'title'; 
		$dir   = in_array(strtolower($_GET['dir']), array('asc', 'desc')) ? strtolower($_GET['dir']) : 'asc';
		$callback = $_GET['callback'];

		$records = Excursion::model()
						->criteria($sort, $dir, $start, $limit)
						->findAll('excursionTypeId=:excursionTypeId', array(':excursionTypeId' => $typeId));

		$count = Excursion::model()
						->count('excursionTypeId=:excursionTypeId', array(':excursionTypeId' => $typeId));
		
		$this->renderPartial('json', array(
			'typeId' => $typeId,
			'data'   => $records,
			'count'  => $count,
			'callbackFunction' => $callback
		), false, true);
	}
	
	public function actionCreate()
	{
		$this->setPageTitle('Экскурсии');
		
		$excursionTypes = CActiveRecordData::getArray(
			ExcursionType::model()->criteria('id', 'asc')->findAll(),
			array('id', 'title'), false
		);
		
		$this->render('create', array(
			'excursionTypes' => $excursionTypes
		));
	}
	
	public function actionCreatehandler()
	{
		$excursionTypes = CActiveRecordData::getList(
			ExcursionType::model()->criteria('id', 'asc')->findAll(),
			'id'
		);
		
		$excursionTypeId = intval($_POST['Excursion']['excursionTypeId']);
		
		if (in_array($excursionTypeId, $excursionTypes)) {
			$excursion = new Excursion();
			$excursion->attributes = array(
				'excursionTypeId' => $excursionTypeId,
				'title'  => $_POST['Excursion']['title'],
				'price'  => ((array_key_exists('price' . $excursionTypeId, $_POST['Excursion'])) ? $_POST['Excursion']['price' . $excursionTypeId] : 0),
				'price2' => ((array_key_exists('price' . $excursionTypeId . 'Shek', $_POST['Excursion'])) ? $_POST['Excursion']['price' . $excursionTypeId . 'Shek'] : 0),
				'route'  => $_POST['Excursion']['route' . $excursionTypeId]
			);
			
			if ($excursion->save() && $excursionTypeId == 1) {
				$excursion->saveAttractions($excursion->id, (array_key_exists('Attraction', $_POST) ? $_POST['Attraction'] : array()));
				
				FlashMessage::saveInfo(array(
					'title'  => 'Инфо',
					'text'   => 'Экскурсия сохранена'
				));
			}
		}
		
		$this->redirect('/admin/excursion/');
	}
	
	public function actionEdit()
	{
		$this->setPageTitle('Экскурсии');
		
		$id = intval($_GET['id']);
		
		if ($id > 0) {
			$excursion = Excursion::model()->with(
				array('attractions')
			)->findByPk($id);
			
			$excursionTypes = CActiveRecordData::getArray(
				ExcursionType::model()->criteria('id', 'asc')->findAll(),
				array('id', 'title'), false
			);
		
			$this->render('edit', array(
				'excursion'      => $excursion,
				'attractions'    => $excursion->getRelated('attractions'),
				'excursionTypes' => $excursionTypes
			));
		}
	}
	
	public function actionEdithandler()
	{
		$excursionId = intval($_GET['id']);
		
		$excursion = new Excursion();
		
		if (isset($_POST['Excursion'])) {
			if (Excursion::model()->findByPk($excursionId)) {
				
				$excursionTypeId = intval($_POST['Excursion']['excursionTypeId']);
				$excursionTypes  = CActiveRecordData::getList(ExcursionType::model()->criteria('id', 'asc')->findAll(), 'id');
				
				if (in_array($excursionTypeId, $excursionTypes)) {
					$data = array(
						'excursionTypeId' => $excursionTypeId,
						'title'  => $_POST['Excursion']['title'],
						'price'  => ((array_key_exists('price' . $excursionTypeId, $_POST['Excursion'])) ? $_POST['Excursion']['price' . $excursionTypeId] : 0),
						'price2' => ((array_key_exists('price' . $excursionTypeId . 'Shek', $_POST['Excursion'])) ? $_POST['Excursion']['price' . $excursionTypeId . 'Shek'] : 0),
						'route'  => $_POST['Excursion']['route' . $excursionTypeId]
					);
					
					Excursion::model()->updateByPk($excursionId, $data);
					$excursion->saveAttractions($excursionId, (array_key_exists('Attraction', $_POST) ? $_POST['Attraction'] : array()));
					
					FlashMessage::saveInfo(array(
						'title'  => 'Инфо',
						'text'   => 'Экскурсия сохранена'
					));
				}
            }
		}
		
		$this->redirect('/admin/excursion/');
	}
	
	public function actionRemove()
	{
		$id = intval($_GET['id']);

		if ($id > 0) {
			Excursion::model()->deleteByPk($id);
			Hotel2excursion::model()->deleteAll('excursionId=:excursionId', array(':excursionId' => $id));
		}
		
		die();
	}
}